package com.example.qixinshangwutong.view.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.view.activity.AboutMeActivity;
import com.example.qixinshangwutong.view.activity.LogOutActivity;
import com.example.qixinshangwutong.view.activity.LoginActivity;
import com.example.qixinshangwutong.view.activity.OpinionActivity;
import com.example.qixinshangwutong.view.activity.WatchHistoryActivity;

import cn.leancloud.LCUser;


public class MyFragment extends Fragment {
    private TextView text_user;
    private LCUser currentUser;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root =  inflater.inflate(R.layout.fragment_my, container, false);
        initView(root);

        return root;
    }

    public void initView(View root){

        LinearLayout imageView = root.findViewById(R.id.about_me);
        LinearLayout fb = root.findViewById(R.id.feedback);
        LinearLayout set = root.findViewById(R.id.set);
        text_user=root.findViewById(R.id.text_user);
        LinearLayout Ll_ls=root.findViewById(R.id.Ll_ls);
        currentUser=LCUser.getCurrentUser();
        if ((currentUser!=null)){
            text_user.setText(currentUser.getUsername());
        }else {
            text_user.setText("点击登录");
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),AboutMeActivity.class);
                startActivity(intent);
            }
        });
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUser!=null){
                    Intent intent=new Intent(getActivity(), LogOutActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        text_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUser!=null){
                    Intent intent=new Intent(getActivity(),LogOutActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        Ll_ls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUser!=null){
                    Intent intent=new Intent(getActivity(), WatchHistoryActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUser!=null){
                    Intent intent=new Intent(getActivity(),OpinionActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });

    }

    @Override
    public void onResume() {
        currentUser = LCUser.getCurrentUser();
        if (currentUser != null) {
            text_user.setText(currentUser.getUsername());
        } else {
            text_user.setText("点击登录");
        }
        super.onResume();
    }
}